package com.sts.common.result;

import lombok.Data;
import org.slf4j.MDC;
import org.springframework.http.HttpStatus;

@Data
public class Result<T> {
    private String traceId = MDC.get("traceId"); // 链路追踪
    private Integer code;  // 业务状态码（0=成功，非0=失败）
    private String message; // 提示信息
    private T data;        // 响应数据
    private Integer httpStatus; // HTTP状态码（可选，默认200）

    // 快速成功（无数据）
    public static Result<Void> success() {
        return new Result<>(0, "操作成功", null, HttpStatus.OK.value());
    }

    // 快速成功（带数据）
    public static <T> Result<T> success(T data) {
        return new Result<>(0, "操作成功", data, HttpStatus.OK.value());
    }

    // 快速失败（支持泛型）
    public static <T> Result<T> error(String message) {
        return new Result<>(1, message, null, HttpStatus.BAD_REQUEST.value());
    }

    // 快速失败（带数据）
    public static <T> Result<T> error(String message, T data) {
        return new Result<>(1, message, data, HttpStatus.BAD_REQUEST.value());
    }

    // 全参构造（私有化）
    private Result(Integer code, String message, T data, Integer httpStatus) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.httpStatus = httpStatus;
    }

    // ------------ 链式调用方法 ------------
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> message(String message) {
        this.message = message;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }

    public Result<T> httpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus.value();
        return this;
    }
}